package com.abc.pro186;

import java.util.ArrayList;
import java.util.List;

import com.abc.pro.model.OddRow;

import android.content.Context;
import android.util.Log;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.SectionIndexer;
import android.widget.TextView;


public class OddAdapter extends BaseAdapter implements OnScrollListener, SectionIndexer{
	private Context mContext;
	private LayoutInflater mInflater;
	ArrayList<Pair<String, ArrayList<OddRow>>> all;
	/**
	 * Pinned header state: don't show the header.
	 */
	public static final int PINNED_HEADER_GONE = 0;

	/**
	 * Pinned header state: show the header at the top of the list.
	 */
	public static final int PINNED_HEADER_VISIBLE = 1;

	/**
	 * Pinned header state: show the header. If the header extends beyond the
	 * bottom of the first shown element, push it up and clip.
	 */
	public static final int PINNED_HEADER_PUSHED_UP = 2;

	/**
	 * Computes the desired state of the pinned header for the given position of
	 * the first visible list item. Allowed return values are
	 * {@link #PINNED_HEADER_GONE}, {@link #PINNED_HEADER_VISIBLE} or
	 * {@link #PINNED_HEADER_PUSHED_UP}.
	 */
	public int getPinnedHeaderState(int position) {
		if (position < 0 || getCount() == 0) {
			return PINNED_HEADER_GONE;
		}

		// The header should get pushed up if the top item shown
		// is the last item in a section for a particular letter.
		int section = getSectionForPosition(position);
		int nextSectionPosition = getPositionForSection(section + 1);
		if (nextSectionPosition != -1 && position == nextSectionPosition - 1) {
			return PINNED_HEADER_PUSHED_UP;
		}

		return PINNED_HEADER_VISIBLE;
	}

	public OddAdapter(ArrayList<Pair<String, ArrayList<OddRow>>> all, Context mContext) {
		super();
		this.all = all;
		this.mContext = mContext;
		this.mInflater = LayoutInflater.from(mContext);
	}

	protected void bindSectionHeader(View view, int position,
			boolean displaySectionHeader) {
		if (displaySectionHeader) {
			view.findViewById(R.id.lblSectionHeader)
					.setVisibility(View.VISIBLE);
			TextView lSectionTitle = (TextView) view
					.findViewById(R.id.lblSectionHeader);
			lSectionTitle
					.setText(getSections()[getSectionForPosition(position)]);
		} else {
			view.findViewById(R.id.lblSectionHeader).setVisibility(View.GONE);
		}
	}

	public int getPositionForSection(int section) {
		if (section < 0)
			section = 0;
		if (section >= all.size())
			section = all.size() - 1;
		int c = 0;
		for (int i = 0; i < all.size(); i++) {
			if (section == i) {
				return c;
			}
			c += all.get(i).second.size();
		}
		return 0;
	}

	public int getSectionForPosition(int position) {
		int c = 0;
		for (int i = 0; i < all.size(); i++) {
			if (position >= c && position < c + all.get(i).second.size()) {
				return i;
			}
			c += all.get(i).second.size();
		}
		return -1;
	}

	public String[] getSections() {
		String[] res = new String[all.size()];
		for (int i = 0; i < all.size(); i++) {
			res[i] = all.get(i).first;
		}
		return res;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		int res = 0;
		for (int i = 0; i < all.size(); i++) {
			res += all.get(i).second.size();
		}
		return res;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		int c = 0;
		for (int i = 0; i < all.size(); i++) {
			if (position >= c && position < c + all.get(i).second.size()) {
				return all.get(i).second.get(position - c);
			}
			c += all.get(i).second.size();
		}
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View res = convertView;
		OddHolderView holder;
		if (res == null){
			res = mInflater.inflate(R.layout.odd_list_row, null);
			holder = new OddHolderView(res);
			res.setTag(holder);
		}
		else{
			holder = (OddHolderView)res.getTag();
		}
		
		final int section = getSectionForPosition(position);
		holder.updateContentWithOddRow((OddRow)getItem(position));
		boolean displaySectionHeaders = (getPositionForSection(section) == position);
		bindSectionHeader(res, position, displaySectionHeaders);
		if((position % 2) == 0)
		{
			res.setBackgroundColor(mContext.getResources().getColor(R.color.odd_dark_row));
		}
		else{
			res.setBackgroundColor(mContext.getResources().getColor(R.color.odd_light_row));
		}
		return res;
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
		if (view instanceof OddListView) {
			((OddListView) view).configureHeaderView(firstVisibleItem);
		}
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
	}
	public void configurePinnedHeader(View header, int position, int alpha) {
		TextView lSectionHeader = (TextView)header;
		lSectionHeader.setText(getSections()[getSectionForPosition(position)]);
		lSectionHeader.setBackgroundColor(alpha << 24 | mContext.getResources().getColor(R.color.odd_section_header));
		lSectionHeader.setTextColor(alpha << 24 | (0x000000));
	}
	protected class OddHolderView{
		//first column
		public TextView tvDate;
		public TextView tvTime;
		//2nd column
		public TextView tvHomeTeam;
		public TextView tvAwayTeam;
		public TextView tvResult;
		//full
		//1x2
		public TextView tvFull1x2Home;
		public TextView tvFull1x2Away;
		public TextView tvFull1x2Result;
		//hdp
		public TextView tvFullHDP1;
		public TextView tvFullHDP2_1;
		public TextView tvFullHDP2_2;
		//ou
		public TextView tvFullOU1;
		public TextView tvFullOU2_1;
		public TextView tvFullOU2_2;
		//half
		//1x2
		public TextView tvHalf1x2Home;
		public TextView tvHalf1x2Away;
		public TextView tvHalf1x2Result;
		//hdp
		public TextView tvHalfHDP1;
		public TextView tvHalfHDP2_1;
		public TextView tvHalfHDP2_2;
		//ou
		public TextView tvHalfOU1;
		public TextView tvHalfOU2_1;
		public TextView tvHalfOU2_2;
		
		public ImageView imgvPlus;
		public ImageView imgvStats;
		public OddHolderView(View converted){
			if(converted != null){
				this.tvDate = (TextView)converted.findViewById(R.id.lblDate);
				this.tvTime = (TextView)converted.findViewById(R.id.lblTime);
				this.tvHomeTeam = (TextView)converted.findViewById(R.id.lblHomeTeam);
				this.tvAwayTeam = (TextView)converted.findViewById(R.id.lblAwayTeam);
				this.tvResult = (TextView)converted.findViewById(R.id.lblResult);
				
				this.tvFull1x2Home = (TextView)converted.findViewById(R.id.lbl_full_1x2_home_team);
				this.tvFull1x2Away = (TextView)converted.findViewById(R.id.lbl_full_1x2_away_team);
				this.tvFull1x2Result = (TextView)converted.findViewById(R.id.lbl_full_1x2_result);
				
				this.tvFullHDP1 = (TextView)converted.findViewById(R.id.lbl_full_hdp_1);
				this.tvFullHDP2_1 = (TextView)converted.findViewById(R.id.lbl_full_hdp_2_1);
				this.tvFullHDP2_2 = (TextView)converted.findViewById(R.id.lbl_full_hdp_2_2);
				
				this.tvFullOU1 = (TextView)converted.findViewById(R.id.lbl_full_ou_1);
				this.tvFullOU2_1 = (TextView)converted.findViewById(R.id.lbl_full_ou_2_1);
				this.tvFullOU2_2 = (TextView)converted.findViewById(R.id.lbl_full_ou_2_2);

				this.tvHalf1x2Home = (TextView)converted.findViewById(R.id.lbl_half_1x2_home_team);
				this.tvHalf1x2Away = (TextView)converted.findViewById(R.id.lbl_half_1x2_away_team);
				this.tvHalf1x2Result = (TextView)converted.findViewById(R.id.lbl_half_1x2_result);
				
				this.tvHalfHDP1 = (TextView)converted.findViewById(R.id.lbl_half_hdp_1);
				this.tvHalfHDP2_1 = (TextView)converted.findViewById(R.id.lbl_half_hdp_2_1);
				this.tvHalfHDP2_2 = (TextView)converted.findViewById(R.id.lbl_half_hdp_2_2);
				
				this.tvHalfOU1 = (TextView)converted.findViewById(R.id.lbl_half_ou_1);
				this.tvHalfOU2_1 = (TextView)converted.findViewById(R.id.lbl_half_ou_2_1);
				this.tvHalfOU2_2 = (TextView)converted.findViewById(R.id.lbl_half_ou_2_2);
				
				this.imgvPlus = (ImageView)converted.findViewById(R.id.imgvPlus);
				this.imgvStats = (ImageView)converted.findViewById(R.id.imgvStats);
						
			}
		}
		public void updateContentWithOddRow(OddRow row){
		    tvHomeTeam.setText(row.getHomeTeam());
		    tvAwayTeam.setText(row.getAwayTeam());
		    tvResult.setText(R.string.match_tie);
		    
			tvDate.setText(row.getDate());
			tvTime.setText(row.getTime());
			
			tvFull1x2Result.setText(row.getFull1x2Result());
			tvFull1x2Home.setText(row.getFull1x2Home());
			tvFull1x2Away.setText(row.getFull1x2Away());
			
			tvFullHDP1.setText(row.getFullhdp1());
			tvFullHDP2_1.setText(row.getFullhdp2_1());
			tvFullHDP2_2.setText(row.getFullhdp2_2());
			
			tvFullOU1.setText(row.getFullou1());
			tvFullOU2_1.setText(row.getFullou2_1());
			tvFullOU2_2.setText(row.getFullou2_2());
			
			tvHalf1x2Result.setText(row.getHalf1x2Result());
            tvHalf1x2Home.setText(row.getHalf1x2Home());
            tvHalf1x2Away.setText(row.getHalf1x2Away());
            
            tvHalfHDP1.setText(row.getHalfhdp1());
            tvHalfHDP2_1.setText(row.getHalfhdp2_1());
            tvHalfHDP2_2.setText(row.getHalfhdp2_2());
            
            tvHalfOU1.setText(row.getHalfou1());
            tvHalfOU2_1.setText(row.getHalfou2_1());
            tvHalfOU2_2.setText(row.getHalfou2_2());
            
            if(row.isShowPlus()){
            	imgvPlus.setVisibility(View.VISIBLE);
            }
            else{
            	imgvPlus.setVisibility(View.INVISIBLE);
            }
            if(row.isShowStats()){
            	imgvStats.setVisibility(View.VISIBLE);
            }
            else{
            	imgvStats.setVisibility(View.INVISIBLE);
            }
		}
	}
}
